<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>state</title>
</head>

<body>
    <h1>state</h1>
    <ol>
        <li>setState()方法第二个参数可以传递一个回调函数</li>
        <li>字符串标签插入：dangerouslySetInnerHTML={{__html:HTML字符串}}</li>
    </ol>
    <hr />
    <div id="demoReact"></div>
    <script src="../../node_modules/babel-standalone/babel.min.js"></script>
    <script src="../../node_modules/react/umd/react.development.js"></script>
    <script src="../../node_modules/react-dom/umd/react-dom.development.js"></script>
    <script type="text/babel">

        class MyCom extends React.Component {

            // 在ES6中，不管子类写不写constructor，new的时候都会补上constructor
            // 我们可以不写，但是我们写了，必须写上super()，作用是调用父类构造方法
            // 如果想在constructor中使用props那么必须写上super(props)
            constructor(props) {
                super(props)

                this.state = {
                    name: "奉先",
                    age: 33,
                    newHtml: "<p>我是一个P标签</p>"
                }
            }

            fun = () => {
                this.setState({ name: "Hydra2" }, () => {
                    console.log("this.state.name = " + this.state.name);
                });
                console.log("this.state.name = " + this.state.name);
            }

            render() {
                return (
                    <div>
                        <button onClick={this.fun}>点击我改变数据</button>
                        <div>我是一个类组件：{this.state.name}</div>
                        <div>{this.state.newHtml}</div>
                        <div dangerouslySetInnerHTML={{ __html: this.state.newHtml }}></div>
                    </div>
                )
            }
        }

        // 创建根节点
        let root = ReactDOM.createRoot(document.getElementById("demoReact"));

        // 渲染组件
        root.render(<MyCom />);
    </script>
</body>

</html>